/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  表单信息提示层
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-29
 * @example
 */
$require('form.cFormTips')
$require('form.formInputText')

LJS.define('form.cFormTitle',function(ljs){

    var cFormTips = ljs.form.cFormTips;
    var formInputText = ljs.form.formInputText;
    var formTitleTpl = {
        tip: '最多不要超过20个汉字',
        error: '最多不要超过20个汉字'
    };
    
    
    function formTitle(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        that.el = opt.el;
		that.tipsEl = opt.tipsEl || opt.el;   // 要弹出的提示层位置
        that.require = opt.require || [];
        that.value = that.el.value || null;
        var formData = that.el.getAttribute('form_data');
        that.data = typeof formData === 'string' ? $.queryToJson(formData) : {};
        that.min = parseInt(that.data.min) || '';
        that.max = parseInt(that.data.max) || '';
        that.formTitleTpl = opt.formTitleTpl || formTitleTpl;
        // 是否实时判断合法性
        that.changeStore = parseInt(that.data.isChange) || 0;
        that.isChange = 0;
        that.state = false;
        that.arrow = opt.arrow;
        that.posObj = opt.posObj;
        that.isMust = parseInt(that.data.isMust) || 0;
        that.isChe = that.data.isChe || 0;
        that.init();
    }
    formTitle.prototype = new formInputText;
    formTitle.prototype.constructor = formInputText
    formTitle.prototype.init = function(){
        var that = this;
        that.value !== null ? that.setValue(that.value) : '';
        that.tip = new cFormTips({
            'el': that.tipsEl || that.el,
            'arrow' : that.arrow,
            'posObj' : that.posObj
        });
        $.foreach(that.require, function(a, i){
            switch (a) {
                case 'focus':
                    that.focus({
                        callback: function(txtLength){
                            that.state === true ? '' : that.showTip();
                        },
                        loop: function(txtLength){
                            that.check() ? that.hide() : that.showError();
                        }
                    });
                    break;
                case 'blur':
                    that.blur({
                        callback: function(){
                            that.setValue(that.integerNum(that.getValue()));
                            if (that.check()) {
                                that.isChange = 0;
                                that.showRight();
                                that.state = false;
                                that.setValue(that.getValue());
                            }
                            else {
                                that.showError();
                                $.addClassName(that.el, 'error');
                                that.isChange = that.changeStore;
                                that.state = true;
                            }
                        }
                    });
                    break;
                case 'select':
                    that.select({
                        callback: function(){
                        }
                    });
                    break;
                default:
                    return false;
            }
        });
    };
    
    /**
     * 检测title是否符合要求
     */
    formTitle.prototype.check = function(){
        var that = this;
        var txtLength = that.getValueLength();
        if(that.isChe){
            if(!that.checkChinese()){
                return false
            }
        };
		if(that.getValue()==that.el.getAttribute('definevalue')){
			return false
			}
		
        if (that.isMust) {
            that.min = that.data.min || 1;
            that.max = that.data.max || 10000;
            return txtLength <= that.max && txtLength >= that.min;
        }
        else 
            that.min = that.data.min || 0;
        that.max = that.data.max || 10000;
        return txtLength <= that.max && txtLength >= that.min;
        
    };
    formTitle.prototype.checkChinese = function(){
        var that = this;
        var value = that.getValue();
        if(/[^\u4E00-\u9FA5]/g.test(value)) return false
        return /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3])*$/g.test(value)
        //return true
    };
    formTitle.prototype.integerNum = function(str){
        str = $.trim(str);
        return str;
    }
    // 显示提示层
    formTitle.prototype.showTip = function(txt){
        var that = this;
        that.tip.show(txt || that.formTitleTpl.tip);
    };
    // 隐藏提示层
    formTitle.prototype.hide = function(){
        var that = this;
        $.removeClassName(that.el, 'error');
        that.tip.hide();
    };
    // 显示错误提示层
    formTitle.prototype.showError = function(txt){
        var that = this;
        that.tip.show(txt || that.formTitleTpl.error,1);
        $.addClassName(that.el, 'error');
    };
    formTitle.prototype.showRight = function(txt){
     var that = this;
     that.tip.show(txt || '',2);
	 $.addClassName(that.el,'error');
    };
    /**
     * 设置form_datad 值
     * 	J2Q();
     */
    formTitle.prototype.J2Q = function(){
        var that = this;
        var opt = {
            'max': that.max || '',
            'min': that.min || '',
            'isChange': that.isChange || 0,
            'isMust': that.isMust || 0
        }
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    return formTitle;
});
